package io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Test04 {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        //1.创建文件对象
        // 被复制的文件
        File fromFile = new File("D:/1.txt");
        // 复制出来的文件
        File toFile = new File("D:/4.txt");
        //2.创建字符输入流
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader(fromFile);
            writer = new FileWriter(toFile);
            //3.循环读取（打印）
            int content = reader.read();
            while (content != -1) {
                //将字符写入文件
                writer.write(content);
                //继续读取
                content = reader.read();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                reader.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("复制完成，耗时:" + (endTime - startTime) + "毫秒");


    }

}